﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WW;
using WW.Cad.Model;
using System.IO;
using WW.Cad.IO;


namespace StuertzDatenbank
{
    public partial class ZeichnungladenGui : Form
    {
        private Sprachfile sprachfile;
        private ViewControl viewcontrol;
        private Profilgui profilgui;
        private DxfModel model;
        
        
        public ZeichnungladenGui(Profilgui Profilgui)
             
        {
            sprachfile = new Sprachfile();
            viewcontrol = new ViewControl();
            profilgui = Profilgui;
            model = new WW.Cad.Model.DxfModel();

            InitializeComponent();
            InitializeComponentChange();
            
            
            

        }

        private void InitializeComponentChange()
        {
            this.dataGridView1.AllowUserToAddRows = false;
            this.button1.Text = sprachfile.Dateioeffnen;
            this.Text = sprachfile.ZeichnungLaden;
            this.Datei.Text = sprachfile.Datei;

           for (int i = 0; i < sprachfile.LayerType.Count; i++)
			{
                this.TypSp.Items.Add(sprachfile.LayerType[i]);
			}
	{
		 
	}
           

            // 
           //Viewcontrol
            // 
            this.viewcontrol.AutoSize = true;
            this.tableLayoutPanel1.SetColumnSpan(viewcontrol, 8);
            this.tableLayoutPanel1.SetRowSpan(viewcontrol, 1);
            this.viewcontrol.Dock = System.Windows.Forms.DockStyle.Fill;
            this.viewcontrol.Location = new System.Drawing.Point(3, 1);
            this.viewcontrol.Name = "Datei";
            this.viewcontrol.Size = new System.Drawing.Size(190, 26);
            this.viewcontrol.TabIndex = 1;
            this.viewcontrol.Visible = true;
            this.tableLayoutPanel1.Controls.Add(this.viewcontrol, 1, 1);
            

        }
        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool okay = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new
                   System.IO.StreamReader(openFileDialog1.FileName);
                
                this.Datei.Text = openFileDialog1.FileName;
                sr.Close();

                

                string extension = Path.GetExtension(openFileDialog1.FileName);
                if (string.Compare(extension, ".dwg", true) == 0)
                {
                    model = DwgReader.Read(openFileDialog1.FileName);
                    okay = true;
                }
                else
                {
                    model = DxfReader.Read(openFileDialog1.FileName);
                    okay = true;
                }

                if (okay)
                {
                    viewcontrol.Model = model;
                }
               
                
                
                this.profilgui.profil.setZeichnung(model);
                this.dataGridView1.Rows.Add(model.Layers.Count);
                for (int i = 0; i < model.Layers.Count; i++)
                {
                    
                    this.dataGridView1.Rows[i].SetValues(model.Layers[i].Name, null, null);

                }


                this.Refresh();
               

            }

        }

        private void Okaybutton_Click(object sender, System.EventArgs e)
        {
            DrawingHandler dH = new DrawingHandler();
            
            
            

            for (int i = 0; i < model.Layers.Count; i++)
            {
                if (this.dataGridView1.Rows[i].Cells[0].Value != null && this.dataGridView1.Rows[i].Cells[0].Value.ToString()!="" && this.dataGridView1.Rows[i].Cells[3].Value != null)
	            {
                    if (bool.Parse(this.dataGridView1.Rows[i].Cells[3].Value.ToString()))
                    {
                        //Abfang leerer Name
                        String name = "";
                        try
                        {
                            name = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        }
                        catch (System.Exception)
                        {
                        }
                        DxfModel tmodel = new DxfModel(model);

                        profilgui.profil = dH.zuordnen(dataGridView1.Rows[i].Cells[1].Value.ToString(), name, profilgui.profil, i, tmodel);		 
	               
                    }
                    }
            }
            this.Dispose();
        }


    }
}
